import { defineConfig, loadEnv } from 'vite';
import { resolve } from 'path';
import { wrapperEnv } from './build/utils';
import { createVitePlugins } from './build/plugin';

// https://vitejs.dev/config/
export default ({ command, mode }) => {
	const isBuild = command === 'build';
	const env = loadEnv(mode, process.cwd());
	const viteEnv = wrapperEnv(env);
	// const { VITE_PORT, VITE_PUBLIC_PATH } = viteEnv;
	return defineConfig({
		plugins: createVitePlugins(viteEnv, isBuild),
		resolve: {
			alias: {
				'@': resolve(__dirname, './src'),
				'~@': resolve(__dirname, './src'),
			},
		},
		css: {
			preprocessorOptions: {
				scss: {
					additionalData: `@import '@/styles/index.scss';`,
				},
			},
		},
		server: {
			host: '0.0.0.0',
			port: env.VITE_PORT,
			proxy: {
				'/dev-api': {
					target: 'http://operate.matchbim.cn:20011/operate',
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/dev-api/, ''),
				},
			},
		},
	});
};
